<?php
namespace Model;

use Core\Constant;
use Exception;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;

class UserTable extends AbstractTable
{

    /**
     * @param $id
     * @return User
     */
    public function getUser($id){
        return $this->getById($id);
    }

    /**
     * @param $id
     * @return bool
     */
    public function removeUser($id){
        $user = $this->getUser($id);
        if ($user != null){
            try{
                $user->detachAllRole();
                $this->delete($id);
            }catch (Exception $e){
                \Logger::debug("delete User.id = ".$id);
                return false;
            }
        }
        return true;
    }

    /**
     * @param $userName
     * @return User
     */
    public function getUserByName($userName)
    {
        return $this->getByColumn('username', $userName)->current();
    }

    /**
     * @param $email
     * @return User
     */
    public function getUserByEmail($email)
    {
        return $this->getByColumn('email', $email)->current();
    }


    /**
     * @param $userName
     * @return int
     */
    public function countUserByName($userName)
    {

        $tableGateway = $this->getTableGateway();
        return $tableGateway->select(function(Select $select) use ($userName) {
            $select->columns(array('COUNT(*) as total'))
                ->where(array('username'=>$userName));
        });

    }


    /**
     * @return InputFilter
     */
    public function getInputFilter()
    {
        $filter = new InputFilter();

// Adding a single input
        $filter->add(array(
            'name' => 'username',
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'not_empty',
                ),
                array(
                    'name' => 'string_length',
                    'options' => array(
                        'min' => 5
                    ),
                ),
            ),
        ));

// Adding another input filter what also contains a single input. Merging both.
        $filter->add(array(
            'type' => 'Zend\InputFilter\InputFilter',
            'password' => array(
                'name' => 'password',
                'required' => true,
                'validators' => array(
                    array(
                        'name' => 'not_empty',
                    ),
                    array(
                        'name' => 'string_length',
                        'options' => array(
                            'min' => 5
                        ),
                    ),
                ),
            ),
        ));

        return $filter;
    }

    public function searchUser($isPaging, $offset, $rowPerPage, $orderArr = array('name'), $searchOption){

        $select = $this->getDbSqlSelect();

        if($searchOption['name']){
            $select->where(array('username like "%'.$searchOption['name'].'%"'));
        }

        if($searchOption['role'] && $searchOption['role'] != Constant::$SELECT_ALL_VALUE){
            $select->join(array('ur'=>'user_role'), 'ur.person_id = user.id', array())
                ->where(array('ur.role_id'=>$searchOption['role']));
        }

        if($isPaging){
            $select->offset($offset)->limit($rowPerPage);
        }

        if($orderArr){
            $select->order($orderArr);
        }

        //generate sql string to debug.log file
        \Logger::debug($select->getSqlString($this->tableGateway->adapter->getPlatform()));

        $rows = $this->tableGateway->selectWith($select);
        $result['rows'] = $rows;

        $total = $this->getCount($select);

        $result['total'] = $total;

        return $result;
    }

}